package cn.akwangl.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 统一响应封装.
 *
 * @author youyongkun
 * @date 2021/9/28 9:31
 */
@ApiModel("请求响应")
public class Result<T> {

    /**
     * 定义jackson对象
     */
    private static final ObjectMapper MAPPER = new ObjectMapper();

    /**
     * 响应业务状态
     */
    @ApiModelProperty(value = "业务状态", required = true)
    private String status;

    /**
     * 响应消息
     */
    @ApiModelProperty(value = "响应消息", required = true)
    private String msg;

    /**
     * 响应中的数据
     */
    @ApiModelProperty(value = "业务数据")
    private T data;

    @JsonIgnore
    private String ok;

    public static Result build(String status, String msg, Object data) {
        return new Result(status, msg, data);
    }

    public static Result build(String status, String msg, Object data, String ok) {
        return new Result(status, msg, data, ok);
    }

    public static Result ok(Object data) {
        return new Result(data);
    }

    public static Result ok() {
        return new Result(null);
    }

    public static Result ok(String msg) {
        return Result.build("200", msg, null);
    }

    public static Result errorMsg(String msg) {
        return new Result("500", msg, null);
    }

    public static Result errorMap(Object data) {
        return new Result("501", "error", data);
    }

    public static Result errorTokenMsg(String msg) {
        return new Result("502", msg, null);
    }

    public static Result errorException(String msg) {
        return new Result("555", msg, null);
    }

    public static Result errorUserQQ(String msg) {
        return new Result("556", msg, null);
    }

    public Result() {

    }

    public Result(String status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public Result(String status, String msg, T data, String ok) {
        this.status = status;
        this.msg = msg;
        this.data = data;
        this.ok = ok;
    }

    public Result(T data) {
        this.status = "200";
        this.msg = "OK";
        this.data = data;
    }

    public Boolean isOK() {
        return this.status == "200";
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getOk() {
        return ok;
    }

    public void setOk(String ok) {
        this.ok = ok;
    }

}
